﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.BackgroundAudio;
using System.Diagnostics;
using System.ComponentModel;
using Puddy.Utils;

namespace Puddy
{
    public partial class AlarmPage : PhoneApplicationPage
    {
        bool isLanchFromBackground = false;

        public AlarmPage()
        {
            InitializeComponent();

             
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (App.ShouldShowMessTurnOffLocation)
            {
                App.ShouldShowMessTurnOffLocation = false;
                MessageBox.Show(Constants.MES_TURN_OFF_LOCATION);

            }

            //reset flag
            Debug.WriteLine("App.RunningAlarm = false");
            App.RunningAlarm = false;

            tbl_name.Text = "Entering " + App.NameAlarm + " area.";
            tbl_address.Text = App.AddressAlarm;            

            string isFromBG;
            if (NavigationContext.QueryString.TryGetValue("IsFromBG", out isFromBG))
            {
                if (isFromBG == "true")
                    isLanchFromBackground = true;
            }

            
        }


        private void btn_dismiss_Click(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("App.RunningAlarm = false");
            //reset flag
            App.RunningAlarm = false;

            if (BackgroundAudioPlayer.Instance != null)
            {

                BackgroundAudioPlayer.Instance.Stop();

            }

            this.NavigationService.Navigate(new Uri("/MainPage.xaml?IsFromAlarmPage=" + "true", UriKind.Relative));
            
        }

        protected  override void OnBackKeyPress(CancelEventArgs e)
        {
            Debug.WriteLine("App.RunningAlarm = false");
            //reset flag
            App.RunningAlarm = false;

            if (BackgroundAudioPlayer.Instance != null)
            {
                BackgroundAudioPlayer.Instance.Stop();
            }

            this.NavigationService.Navigate(new Uri("/MainPage.xaml?IsFromAlarmPage=" + "true", UriKind.Relative));
        }
    }
}